import React, { PureComponent } from 'react';
import { connect } from 'dva';

import { Input, Card, Table, Form, Row, Col, Button, Select, message, Popconfirm, AutoComplete } from 'antd';
import BillEditor from '../Components/BillEditor';

import styles from './styles.less';

const ModelName = "mbl";
@connect(({ loading, tiDanState, relationer, mbl }) => ({
  loading: loading.models[ModelName],
  tiDanStates: tiDanState.list,
  relationers: relationer.list,
  mbl: mbl.current,
}))
@Form.create()
class Comp extends PureComponent {
  state = {
    isHasHBL: false
  }

  handleSubmit = (vm) => {
    const { dispatch } = this.props;
    dispatch({
      type: 'mbl/updateMBL',
      payload: vm,
      callback: resp => {
        if (resp) {
          message.success('保存成功!');
          // this.reloadData();
        }
      }
    })
  }

  componentDidMount() {
    const { dispatch, mbl } = this.props;
    dispatch({
      type: 'mbl/isHasHBL',
      payload: mbl.id,
      callback: resp => {
        this.setState({ isHasHBL: resp });
      }
    })

  }

  handleDownload = (item) => {
    var key = item.objectNumber || item.id;
    window.open(`/api/report/DownloadMBL?key=${key}`, 'blank');
    // window.open(`/api/report/GenMBL?key=${key}`, 'blank');
  }

  render() {
    const { mbl, loading } = this.props;

    const actions = () => (
      <div>
        <a style={{ marginLeft: '8px' }} onClick={(e) => this.handleDownload(mbl)}>下载</a>
      </div>
    );

    return (
      <Card className={styles.card} title={false} loading={loading}>
        <BillEditor bill={{ ...mbl, isHasHBL: this.state.isHasHBL }} actions={actions()} onSave={this.handleSubmit} onPreview={this.handlePrint} />
      </Card>
    );
  }
}

export default Comp;